package cn.wolfcode.crm.service.impl;/**
 * Created by 10797 on 2018/7/12.
 */

import cn.wolfcode.crm.domain.*;
import cn.wolfcode.crm.mapper.*;
import cn.wolfcode.crm.query.GuaranteeBillQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IGuaranteeBillService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.apache.tools.ant.taskdefs.GUnzip;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @program: crm
 * @description:
 * @author: Mr.chen
 * @create: 2018-07-12 09:50
 **/
@Service
public class GuaranteeBillServiceImpl implements IGuaranteeBillService {

    @Autowired
    private GuaranteeBillMapper guaranteeBillMapper;
    @Autowired
    private ClientMapper clientMapper;
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private PaymentBillMapper paymentBillMapper;


    @Override
    public void save(GuaranteeBill guaranteeBill) {

        guaranteeBillMapper.insert(guaranteeBill);
    }

    @Override
    public void delete(Long id) {
        guaranteeBillMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(GuaranteeBill guaranteeBill) {
        guaranteeBillMapper.updateByPrimaryKey(guaranteeBill);
    }

    @Override
    public GuaranteeBill get(Long id) {
        return guaranteeBillMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<GuaranteeBill> getAll() {
        return guaranteeBillMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        int count = guaranteeBillMapper.queryForCount(qo);
        if(count == 0){
            return new PageResult();
        }
        List<GuaranteeBill> list = guaranteeBillMapper.queryForList(qo);
        return new PageResult(count,list);
    }

    @Override
    public Client selectClientByBillId(Long id) {
        return clientMapper.selectClientByBillId(id);
    }

    @Override
    public void editGuaranteeBill(GuaranteeBillVO vo) {
        //1.更新车辆信息
        Car car = new Car();
        //1.1更新车辆所有人信息
        car.setOwner(vo.getOwner());
        //1.2更新车牌号码
        car.setNumber(vo.getNumber());
        //1.3更新车辆购买日期
        car.setPurchaseDate(vo.getPurchaseDate());
        //1.4更新车辆品牌
        car.setBrand(vo.getBrand());
        //1.5更新车辆排气量
        car.setGas(vo.getGas());
        //1.6更新车辆价格
        car.setPrice(vo.getPrice());
        //1.7更新车辆分类
        car.setType(vo.isType());
        //1.8更新车辆类型
        car.setLoadSize(vo.getLoadSize());
        //1.9更新车辆id
        car.setId(vo.getCarInfoId());
        //更新车辆
        carMapper.updateByPrimaryKey(car);

        //2.更新产品信息,在更新产品信息之前先删除之前中间表的信息
        //2.1根据当前保单编号删除保单跟产品的关系
        productMapper.deleteBillAndProductRelation(vo.getSnNumber());
        //2.2更新产品信息,此时的vo.getNumber是页面传输过来修改过的内容
        for (Long id : vo.getIds()) {
            productMapper.saveBillAndProductRelation(vo.getSnNumber(),id);
        }
        //3.更新暂存单信息
        //3.1由于暂存单表当中编码类型是字符串,所以在这里我们先转换成字符串再传输进去
        String snNumber = String.valueOf(vo.getSnNumber());
        //3.2根据当前保单编码查询保单对象
        GuaranteeBill guaranteeBill=guaranteeBillMapper.getBillBySn(snNumber);
        //3.3设置当前保单年限
        guaranteeBill.setLimitYear(vo.getLimitYear());
        //3.4设置当前总金额
        guaranteeBill.setTotalPrice(BigDecimal.valueOf(vo.getTotalAmount()));
        //3.5更新当前暂存单信息
        guaranteeBillMapper.updateBill(guaranteeBill);
    }

    @Override
    public void changeStateUnaudited(Long id) {
        guaranteeBillMapper.changeStateUnaudited(id);
    }

    @Override
    public void changeBillState(String number) {
        guaranteeBillMapper.changeBillState(number);
    }

    @Override
    public void backToChangeBill(String number, String info) {
        guaranteeBillMapper.backToChangeBill(number,info);
    }

    @Override
    public void auditBill(Long id) {
        //1.根据id查询当前保单信息,然后开始更新
        GuaranteeBill guaranteeBill = guaranteeBillMapper.selectByPrimaryKey(id);
        //2.设置审核人和审核时间
        Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        guaranteeBill.setAuditor(currentUser);
        guaranteeBill.setAuditDate(new Date());
        //3.创建缴费单据对象
        PaymentBill paymentBill = new PaymentBill();
        //4.设置投保人
        paymentBill.setPolicyClient(guaranteeBill.getPolicyClient());
        //4.1设置保单编号
        paymentBill.setNumber(guaranteeBill.getNumber());
        //4.2设置保单总金额
        paymentBill.setPrice(guaranteeBill.getTotalPrice());
        //4.3设置保单缴费状态
        paymentBill.setState(PaymentBill.NO_PAY);
        //4.4设置保单年限
        paymentBill.setLimitYear(guaranteeBill.getLimitYear());
        //声明当前公司对象,按理直接声明即可,但是为了防止空指针异常的发生
        Company company = new Company();
        //5.保存当前保单的保险公司
        // 5.1通过当前保单id获取所有的保险产品信息
        List<Product> products = productMapper.selectProductListByBillNumber(Long.valueOf(guaranteeBill.getNumber()));
        if (products.size() > 0){
            //获得第一个保单产品
            Product product=products.get(0);
            //5.2通过产品获得当前产品的公司
            company=product.getCompany();
        }
        //5.3设置保单公司
        paymentBill.setCompany(company);
        //6.设置核保日期,为审核的日期
        paymentBill.setUnderwritingDate(guaranteeBill.getAuditDate());
        //7.改变当前保单状态为已审核状态
        guaranteeBillMapper.changeStateSuccessfulAudit(guaranteeBill);
        //8.保存当前的缴费单据
        paymentBillMapper.insert(paymentBill);

    }

    @Override
    public GuaranteeBill queryInfo(Long id) {
        return guaranteeBillMapper.queryInfo(id);
    }

    @Override
    public PageResult queryHistoryBill(GuaranteeBillQueryObject qo) {
        int count=guaranteeBillMapper.queryForCount(qo);
        if(count == 0){
            return new PageResult();
        }
        List<GuaranteeBill> list=guaranteeBillMapper.queryHistoryForList(qo);
        return new PageResult(count,list);
    }


}
